﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Guess 
{
    public partial class TopList : System.Web.UI.Page
    {
        protected int userID = 28;//暂时测试用户28
        protected string sort = "cash";//排序方式
        protected void Page_Load(object sender, EventArgs e)
        {
            string struserIDFromCookie = Common.Cookie.getCookie("userID");
            if (string.IsNullOrEmpty(struserIDFromCookie))
            {
                Response.Redirect("/");
            }
            else
            {
                userID = int.Parse(struserIDFromCookie);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                sort = Request.QueryString["s"];                //排行方式
            }
            PageLoad();      //加载页面
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void PageLoad()
        {
            Model.User_Main userModel = new BLL.User_Main().GetModel(userID);

            StringBuilder sbHtml = new StringBuilder(Cache["guess_index"].ToString());//主sb

            //公共部分
            sbHtml = sbHtml.Replace("$Javascript$", Cache["guess_js"].ToString());//JS
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友
            sbHtml = sbHtml.Replace("$GuessLogo$", Cache["guess_logo"].ToString());//竞猜LOGO

            sbHtml = sbHtml.Replace("$GuessMenu$", Cache["guess_menu"].ToString());//竞猜菜单
            sbHtml = sbHtml.Replace("$IndexClass$", "")//按钮样式
                .Replace("$AddClass$", "")
                .Replace("$FriendsClass$", "")
                .Replace("$MyOriClass$", "")
                .Replace("$MyParClass$", "")
                .Replace("$TopClass$", "class=sel")
                .Replace("$Cash$", Convert.ToInt32(userModel.Cash).ToString("n0"));

            sbHtml = sbHtml.Replace("$Content$", Cache["guess_toplist"].ToString());//竞猜排行榜
            string ie= Cache["guess_ie"].ToString();
            if (sort.Equals("cash") || sort == "")
            {
                sbHtml.Replace("$CashClass$", "class = \"sel\"");
                sbHtml.Replace("$CashIE$", ie);
                sbHtml.Replace("$OddsClass$", "");
                sbHtml.Replace("$OddsIE$", "");
            }
            else
            {
                sbHtml.Replace("$CashClass$", "");
                sbHtml.Replace("$CashIE$", "");
                sbHtml.Replace("$OddsClass$", "class = \"sel\"");
                sbHtml.Replace("$OddsIE$", ie);
            }

            sbHtml = sbHtml.Replace("$GuessTopListLi$", getGuessTopList());//竞猜排行li

            Response.Write(sbHtml.ToString());
        }

        /// <summary>
        /// 获取竞猜排行HTML
        /// </summary>
        /// <param name="userModel">用户实体类</param>
        /// <returns>HTMLString</returns>
        protected string getGuessTopList()
        {
            StringBuilder sbHtml = new StringBuilder();
  
            if (sort.Equals("cash"))
            {
                sbHtml.Append(getTopList("现金"));
            }
            else if (sort.Equals("odds"))
            {
                sbHtml.Append(getTopList("猜中率"));
            }

            return sbHtml.ToString();
        }

        /// <summary>
        /// 按排行类型获取竞猜排行
        /// </summary>
        /// <param name="SortType">排行类型("现金"or"猜中率")</param>
        /// <returns>字符串</returns>
        protected string getTopList(string SortType)
        {
            StringBuilder sbHtml = new StringBuilder();

            string strLi = Cache["guess_toplistli"].ToString();

            DataSet ds = new BLL.Guess.GuessSql().TopList(userID, SortType);

            DataTable dt = ds.Tables[0];

            int rowsCount = dt.Rows.Count;

            for (int i = 0; i < rowsCount; i++)
            {
                string str = strLi;

                str = str.Replace("$UserID$", dt.Rows[i]["用户ID"].ToString())
                .Replace("$GuessUserName$", dt.Rows[i]["用户名"].ToString())
                .Replace("$GuessUserImg$", new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dt.Rows[i]["用户ID"])))
                .Replace("$GuessCash$", Convert.ToInt32(dt.Rows[i]["现金"]).ToString("n0"))
                .Replace("$Odds$", Convert.ToInt32(dt.Rows[i]["猜中率"]).ToString(""))
                .Replace("$TopCount$", (i+1).ToString());

                sbHtml.Append(str);
            }

            return sbHtml.ToString();
        }

    }
}
